package com.bright.ghj.overt.service.impl;

import com.bright.ghj.overt.converter.MainModalConverter;
import com.bright.ghj.overt.converter.NavigateConverter;
import com.bright.ghj.overt.converter.WebMenuConverter;
import com.bright.ghj.overt.enums.NavigateItem;
import com.bright.ghj.overt.manager.FileListManager;
import com.bright.ghj.overt.manager.UserManager;
import com.bright.ghj.overt.manager.WebMenuManager;
import com.bright.ghj.overt.pojo.dto.MainModalDTO;
import com.bright.ghj.overt.pojo.dto.NavigateDTO;
import com.bright.ghj.overt.pojo.dto.UserDTO;
import com.bright.ghj.overt.pojo.dto.WebMenuDTO;
import com.bright.ghj.overt.pojo.vo.NavigateVO;
import com.bright.ghj.overt.pojo.vo.TableTypeVO;
import com.bright.ghj.overt.pojo.vo.WebMenuVO;
import com.bright.ghj.overt.service.NavigateService;
import com.bright.ghj.common.util.ListUtil;
import com.bright.ghj.overt.util.SecurityUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author hxj
 * @Date 2023/12/21 17:50
 * @Description
 */
@Component
@RequiredArgsConstructor
public class NavigateServiceImpl implements NavigateService {

    private final WebMenuManager webMenuManager;
    private final FileListManager fileListManager;

    private final UserManager userManager;
    private final WebMenuConverter webMenuConverter;
    private final MainModalConverter mainModalConverter;
    private final NavigateConverter navigateConverter;

    @Override
    public List<WebMenuVO> listAllWebMenus() {

        List<WebMenuDTO> webMenus = webMenuManager.listWebMenuByCache();
        List<WebMenuVO> webMenuVOS = new ArrayList<>();

        for (WebMenuDTO webMenu : webMenus) {
            WebMenuVO webMenuVO = webMenuConverter.dtoConvertVO(webMenu);
            webMenuVO.setMainModalList(listTableTypesByMenuId(webMenu.getMenuId()));
            webMenuVOS.add(webMenuVO);
        }

        return webMenuVOS;
    }

    @Override
    public List<TableTypeVO> listTableTypesByMenuId(String menuId) {
        List<MainModalDTO> mainModals = fileListManager.listMainModalByMenuIdByCache(menuId);
        List<TableTypeVO> tableTypeVOS = new ArrayList<>();

        // 排除子表不显示
        List<String> notShowSubTableNames = new ArrayList<>();

        for (MainModalDTO mainModalDTO : mainModals) {

            if (notShowSubTableNames.contains(mainModalDTO.getTableName())) {
                continue;
            }

            TableTypeVO tableTypeVO = mainModalConverter.dtoConvertVO(mainModalDTO);
            tableTypeVO.setPublicTypeNo(mainModalDTO.getPublicType().getValue());
            tableTypeVO.setPublicTypeName(mainModalDTO.getPublicType().getDescription());
            tableTypeVO.setDescription(mainModalDTO.getModalName());
            tableTypeVOS.add(tableTypeVO);

            if (mainModalDTO.getSubMainModalList() != null) {
                notShowSubTableNames.addAll(ListUtil.toSingleList(mainModalDTO.getSubMainModalList(), MainModalDTO::getTableName));
            }
        }
        return tableTypeVOS;
    }

//    @Override
//    @Cacheable(value = "navigates", key = "#root.methodName + '_' + #userName")
//    public List<NavigateVO> listAllNavigatesUseOldTable(String userName) {
//
//        List<NavigateVO> navigates = new ArrayList<>();
//        // 都可以看到的“录入”导航
//        //
//        // 增加配置参数 是否显示党务事务录入 由于录入导航不用后台管理权限都能看到 这里由代码增加 党务menuId=21 事务menuId=22
//        NavigateVO dwParentNavigate, swParentNavigate;
//        List<NavigateVO> dwSubNavigates = null, swSubNavigates = null;
//        // 党务
//        if (DataConstant.DW_ENABLE) {
//            dwParentNavigate = new NavigateVO();
//            dwParentNavigate.setDescription("党务数据公开");
//            navigates.add(dwParentNavigate);
//            dwSubNavigates = new ArrayList<>();
//            dwParentNavigate.setSubNavigates(dwSubNavigates);
//            dwSubNavigates.add(buildSubNavigateVO(NavigateItem.DW_VIEW_DATA_EDIT));
//        }
//        // 事务
//        if (DataConstant.SW_ENABLE) {
//            swParentNavigate = new NavigateVO();
//            swParentNavigate.setDescription("事务数据公开");
//            navigates.add(swParentNavigate);
//            swSubNavigates = new ArrayList<>();
//            swParentNavigate.setSubNavigates(swSubNavigates);
//            swSubNavigates.add(buildSubNavigateVO(NavigateItem.SW_VIEW_DATA_EDIT));
//        }
//        // 财务
//        NavigateVO cwParentNavigate = new NavigateVO();
//        cwParentNavigate.setDescription("财务数据公开");
//        List<NavigateVO> cwSubNavigates = new ArrayList<>();
//        cwSubNavigates.add(buildSubNavigateVO(NavigateItem.CW_VIEW_DATA_EDIT));
//        if (DataConstant.CW_ENABLE) {
//            navigates.add(cwParentNavigate);
//            cwParentNavigate.setSubNavigates(cwSubNavigates);
//        }
//
//
//        // 首先判断是否有权限进入后台管理
//        UserDTO user = userManager.getUserByUserName(userName);
//        boolean isPermitAdmin = user.getIsCwgk() != null && user.getIsCwgk();
//        if (!isPermitAdmin) {
//            // 没有进入后台管理的权限 则只能看到录入数据
//            return navigates;
//        }
//
//        // 允许看到后台管理
//        List<AdminMenuDTO> adminMenuList = webMenuManager.listAdminMenuByCache(user);
//        for (AdminMenuDTO parentMenu : adminMenuList) {
//            String parentMenuId = StringUtil.trimToEmpty(parentMenu.getMenuId());
////            String description = StringUtil.trimToEmpty(parentMenu.getShowName());
//
//            // 是否为党务、事务或财务
//            boolean isCW = StringUtil.equals(parentMenuId, "02"), isDW = StringUtil.equals(parentMenuId, "21"), isSW = StringUtil.equals(parentMenuId, "22");
//
//            NavigateVO parentNavigateVO;
//            List<NavigateVO> subNavigateVOS = null;
//            if (!isCW && !isDW && !isSW) {
//                // 如果党务、事务、财务公开的子菜单 则并入相应父级导航栏 否则新增父级导航
//                parentNavigateVO = new NavigateVO();
//                subNavigateVOS = new ArrayList<>();
//                parentNavigateVO.setDescription(parentMenu.getShowName());
//                parentNavigateVO.setSubNavigates(subNavigateVOS);
//                navigates.add(parentNavigateVO);
//            }
//
//            for (AdminMenuDTO subMenu : parentMenu.getSubAdminMenuList()) {
//                String subMenuId = subMenu.getMenuId();
//                if (NavigateItem.exceptMenuIdList.contains(subMenuId)) {
//                    // 跳过旧配置中的不需要再显示出来的导航 如未通过审核 旧统计表等
//                    continue;
//                }
//
//                // 兼容旧配置 根据adminMenu的menuId返回预定义好的menuKey标识 告诉前端该导航跳转什么页面
//                NavigateItem navigateItem = NavigateItem.getByMenuId(subMenuId);
//                if (navigateItem != null) {
//                    NavigateVO newSubNavigate = buildSubNavigateVO(navigateItem);
//                    if (subNavigateVOS != null) {
//                        subNavigateVOS.add(newSubNavigate);
//                    } else {
//                        // 如果党务、事务、财务公开的子菜单 则并入相应父级导航栏
//                        if (isDW && dwSubNavigates != null) {
//                            dwSubNavigates.add(newSubNavigate);
//                        } else if (isSW && swSubNavigates != null) {
//                            swSubNavigates.add(newSubNavigate);
//                        } else {
//                            cwSubNavigates.add(newSubNavigate);
//                        }
//                    }
//
//                } else {
//                    // 在预定义中找不到
//                    NavigateVO subNavigateVO = new NavigateVO();
////                    subNavigateVO.setMenuId(subMenu.getMenuId());
//                    subNavigateVO.setDescription(subMenu.getShowName());
////                    subNavigateVO.setMenuKey(subMenu.getMenuName());
////                    subNavigateVO.setIsDefined(false);
//                    if (subNavigateVOS != null) {
//                        subNavigateVOS.add(subNavigateVO);
//                    } else {
//                        // 如果党务、事务、财务公开的子菜单 则并入相应父级导航栏
//                        if (isDW && dwSubNavigates != null) {
//                            dwSubNavigates.add(subNavigateVO);
//                        } else if (isSW && swSubNavigates != null) {
//                            swSubNavigates.add(subNavigateVO);
//                        } else {
//                            cwSubNavigates.add(subNavigateVO);
//                        }
//                    }
//                }
//
//            }
//        }
//
//        return navigates;
//    }

    @Override
    @Cacheable(value = "navigates", key = "#root.methodName + '_' + #userName")
    public List<NavigateVO> listAllNavigates(String userName) {
//        UserDTO user = userManager.getUserByUserName(userName);
        UserDTO user = SecurityUtil.getLoginUser();
        List<NavigateDTO> navigateDTOS = webMenuManager.listNavigatesByCache(user);
        List<NavigateDTO> allowNavigates;
        List<NavigateVO> result = new ArrayList<>();
        // 获取是否有查看后台管理的权限
        boolean isPermitAdmin = user.getIsPermitAdmin();
        // 若没有权限 则过滤掉后台管理的导航项
        if (!isPermitAdmin) {
            allowNavigates = ListUtil.toListByFilter(navigateDTOS, navigateDTO -> navigateDTO.getIsAdmin() == null || !navigateDTO.getIsAdmin());
        } else {
            allowNavigates = navigateDTOS;
        }

        for (NavigateDTO allowNavigate : allowNavigates) {
            NavigateVO navigateVO = navigateConverter.dtoConvertVO(allowNavigate);
            navigateVO.setSubNavigates(navigateConverter.dtoConvertVO(allowNavigate.getSubNavigateList()));
            result.add(navigateVO);
        }
        return result;
    }

    @Override
    public boolean getUserAbleEdit(String userName) {
        return false;
    }

    private NavigateVO buildSubNavigateVO(NavigateItem navigateItem) {
        NavigateVO navigateVO = new NavigateVO();
//        navigateVO.setMenuKey(navigateItem.getMenuKey());
        navigateVO.setDescription(navigateItem.getDescription());
//        navigateVO.setIsDefined(true);
        return navigateVO;
    }

}
